<?php
# common/image.php
# File hiển thị hình 

header ("Content-type: image/jpeg");
include("configs/define.php");
/****//////////
function ConvertBMP2GD($src, $dest = false) {
if(!($src_f = fopen($src, "rb"))) {
trigger_error("Can't open $src", E_WARNING);
return false;
}
if(!($dest_f = fopen($dest, "wb"))) {
trigger_error("Can't open $dest", E_WARNING);
return false;
}
$header = unpack("vtype/Vsize/v2reserved/Voffset", fread($src_f, 14));
$info =
unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyr
es/Vncolor/Vimportant", fread($src_f, 40));

extract($info);
extract($header);

if($type != 0x4D42) { // signature "BM"
return false;
}

$palette_size = $offset - 54;
$ncolor = $palette_size / 4;
$gd_header = "";
// true-color vs. palette
$gd_header .= ($palette_size == 0) ? "\xFF\xFE" : "\xFF\xFF";
$gd_header .= pack("n2", $width, $height);
$gd_header .= ($palette_size == 0) ? "\x01" : "\x00";
if($palette_size) {
$gd_header .= pack("n", $ncolor);
}
// no transparency
$gd_header .= "\xFF\xFF\xFF\xFF";

fwrite($dest_f, $gd_header);

if($palette_size) {
$palette = fread($src_f, $palette_size);
$gd_palette = "";
$j = 0;
while($j < $palette_size) {
$b = $palette{$j++};
$g = $palette{$j++};
$r = $palette{$j++};
$a = $palette{$j++};
$gd_palette .= "$r$g$b$a";
}
$gd_palette .= str_repeat("\x00\x00\x00\x00", 256 - $ncolor);
fwrite($dest_f, $gd_palette);
}

$scan_line_size = (($bits * $width) + 7) >> 3;
$scan_line_align = ($scan_line_size & 0x03) ? 4 - ($scan_line_size & 0x03)
: 0;

for($i = 0, $l = $height - 1; $i < $height; $i++, $l--) {
// BMP stores scan lines starting from bottom
fseek($src_f, $offset + (($scan_line_size + $scan_line_align) * $l));
$scan_line = fread($src_f, $scan_line_size);
if($bits == 24) {
$gd_scan_line = "";
$j = 0;
while($j < $scan_line_size) {
$b = $scan_line{$j++};
$g = $scan_line{$j++};
$r = $scan_line{$j++};
$gd_scan_line .= "\x00$r$g$b";
}
}
else if($bits == 32) {
$gd_scan_line = "";
$j = 0;
while($j < $scan_line_size) {
$b = $scan_line{$j++};
$g = $scan_line{$j++};
$r = $scan_line{$j++};
$a = chr(255 - ord($scan_line{$j++}));
$gd_scan_line .= "$a$r$g$b";
}
}
else if($bits == 8) {
$gd_scan_line = $scan_line;
}
else if($bits == 4) {
$gd_scan_line = "";
$j = 0;
while($j < $scan_line_size) {
$byte = ord($scan_line{$j++});
$p1 = chr($byte >> 4);
$p2 = chr($byte & 0x0F);
$gd_scan_line .= "$p1$p2";
} $gd_scan_line = substr($gd_scan_line, 0, $width);
}
else if($bits == 1) {
$gd_scan_line = "";
$j = 0;
while($j < $scan_line_size) {
$byte = ord($scan_line{$j++});
$p1 = chr((int) (($byte & 0x80) != 0));
$p2 = chr((int) (($byte & 0x40) != 0));
$p3 = chr((int) (($byte & 0x20) != 0));
$p4 = chr((int) (($byte & 0x10) != 0));
$p5 = chr((int) (($byte & 0x08) != 0));
$p6 = chr((int) (($byte & 0x04) != 0));
$p7 = chr((int) (($byte & 0x02) != 0));
$p8 = chr((int) (($byte & 0x01) != 0));
$gd_scan_line .= "$p1$p2$p3$p4$p5$p6$p7$p8";
} $gd_scan_line = substr($gd_scan_line, 0, $width);
}

fwrite($dest_f, $gd_scan_line);
}
fclose($src_f);
fclose($dest_f);
return true;
}


class MemoryStream {
var $position;
var $varname;
var $buffer;

function stream_open($path, $mode, $options, &$opened_path)
{
$url = parse_url($path);
$this->varname = $url["host"];
$this->position = 0;
$this->buffer = @$GLOBALS[$this->varname];

return true;
}

function stream_close()
{
$GLOBALS[$this->varname] = $this->buffer;
}

function stream_read($count)
{
$ret = substr($this->buffer, $this->position, $count);
$this->position += strlen($ret);
return $ret;
}

function stream_write($data)
{
$this->buffer .= $data;
$this->position += strlen($data);
return strlen($data);
}

function stream_tell()
{
return $this->position;
}

function stream_eof()
{
return $this->position >= strlen($this->buffer);
}

function stream_stat() {
return array( 'size' => strlen($this->buffer) );
}
}

function imagecreatefrombmp($filename) {
// use a memory stream instead of a temp file
// where possible
if(function_exists('stream_wrapper_register')
&& stream_wrapper_register("mem", "MemoryStream")) {
$tmp_name = "mem://GD_TMP_FILE";
$del_tmp = false;
}
else {
$tmp_name = tempnam("/tmp", "GD");
$del_tmp = true;
}
if(ConvertBMP2GD($filename, $tmp_name)) {
$img = imagecreatefromgd($tmp_name);
if($del_tmp) {
unlink($tmp_name);
}
return $img;
} return false;
}



//********************
// Biến
$img = $_GET['url'];
//var_dump($img);
//die();
$percent = $_GET['percent'];
$constrain = $_GET['constrain'];
$w = $_GET['w'];
$h = $_GET['h'];


// Nếu URL có chứa /
if(strpos($img, "/"))
{
	$url_array = explode("/",$img);
	$image_path = $url_array[count($url_array)-1];
	$folder_path = substr($img,0,strlen($img)-strlen($image_path));
	$new_image_path = sprintf('thumb.%s.%s_%s',$w,$h,$image_path);
//	var_dump($url_array,$image_path,$folder_path,$new_image_path);
//	die();
}
else 
{
	$image_path = $img;
	$img=UPLOAD.$img;
	$folder_path = UPLOAD;
	$new_image_path = sprintf('thumb.%s.%s_%s',$w,$h,$image_path);
//	var_dump($folder_path);
//	var_dump($url_array,$image_path,$folder_path,$new_image_path);
	//die();
}

// Nếu đã tồn tại hình thumb thì hiển thị
if(!file_exists($folder_path.$new_image_path))
{
	// get image size of img
	$x = @getimagesize($img);
	if($x)
	{
		// image width
		$sw = $x[0];
		// image height
		$sh = $x[1];
		
		if ($percent > 0) {
			// calculate resized height and width if percent is defined
			$percent = $percent * 0.01;
			$w = $sw * $percent;
			$h = $sh * $percent;
		} else {
			
			if (isset ($w) && !isset ($h)) {
				// autocompute height if only width is set
				$h = (100 / ($sw / $w)) * .01;
				$h = @round ($sh * $h);
			} elseif (isset ($h) && !isset ($w)) {
				// autocompute width if only height is set
				$w = (100 / ($sh / $h)) * .01;
				$w = @round ($sw * $w);
			} elseif (isset ($h) && isset ($w) && isset ($constrain)) {
				// get the smaller resulting image dimension if both height
				// and width are set and $constrain is also set
				$hx = (100 / ($sw / $w)) * .01;
				$hx = @round ($sh * $hx);
		
				$wx = (100 / ($sh / $h)) * .01;
				$wx = @round ($sw * $wx);
		
				if ($hx < $h) {
					$h = (100 / ($sw / $w)) * .01;
					$h = @round ($sh * $h);
				} else {
					$w = (100 / ($sh / $h)) * .01;
					$w = @round ($sw * $w);
				}
			}
			
		}
		
		if($w >= $sw) $w = $sw;
		if($h >= $sh) $h = $sh;
		
		$im = @imagecreatefromjpeg ($img) or // Read JPEG Image
		$im = @imagecreatefrompng ($img) or // or PNG Image
		$im = @imagecreatefromgif ($img) or // or GIF Image
		$im = false; // If image is not JPEG, PNG, or GIF
		
		if (!$im) {
			$ext = substr(strtolower($img),-3);
			if($ext=='bmp'){
				#include_once realpath(dirname(__FILE__) . '/library/') . '/defined/phpthumb.functions.php';
				$new_image_path = substr($new_image_path,0,strlen($new_image_path)-3)."jpg";
				if(!file_exists($folder_path.$new_image_path)){
					$r = imagecreatefrombmp($img);
					#imagepng($r, $folder_path.$new_image_path); 
					#var_dump($new_image_path);
						imagejpeg($r, UPLOAD.$new_image_path);
					//var_dump($r);
					$thumb = @imagecreatetruecolor ($w, $h);
					// Copy from image source, resize it, and paste to image destination
					@imagecopyresampled ($thumb, $r, 0, 0, 0, 0, $w, $h, $sw, $sh);
					// Output resized image
					@imagejpeg ($thumb, $folder_path.$new_image_path, 90);
					imagedestroy($r);
				}
				#var_dump($r);
				
				#$im = @imagecreatefromjpeg ($r) ; // Read JPEG Image
				
				//$thumb = @imagecreatetruecolor ($w, $h);
				// Copy from image source, resize it, and paste to image destination
				//@imagecopyresampled ($thumb, $r, 0, 0, 0, 0, $w, $h, $sw, $sh);
				// Output resized image
				//var_dump($folder_path.$new_image_path);
				//@imagejpeg ($thumb, $folder_path.$new_image_path, 90);
				//$im_thumb = @imagecreatefromjpeg ($folder_path.$new_image_path) or // Read JPEG Image
				//imagedestroy($r);
				//exit();
			}else{
				readfile ($img);
			}
		} else {
			// Create the resized image destination
			$thumb = @imagecreatetruecolor ($w, $h);
			// Copy from image source, resize it, and paste to image destination
			@imagecopyresampled ($thumb, $im, 0, 0, 0, 0, $w, $h, $sw, $sh);
			// Output resized image
			@imagejpeg ($thumb, $folder_path.$new_image_path, 90);
			imagedestroy($im);
		}
	}
}

// Hiển thị hình
$im_thumb = @imagecreatefromjpeg ($folder_path.$new_image_path) or // Read JPEG Image
$im_thumb = @imagecreatefrompng ($folder_path.$new_image_path) or // or PNG Image
$im_thumb = @imagecreatefromgif ($folder_path.$new_image_path) or // or GIF Image
//$im_thumb = @imagecreatefromwbmp ($folder_path.$new_image_path) or // or GIF Image
$im_thumb = false; // If image is not JPEG, PNG, or GIF

imagejpeg($im_thumb,'',90) or 
image2wbmp($im_thumb,'',90);
imagedestroy($im_thumb);
?>
